package com.atguigu.flink.chapter05.source;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
import org.apache.flink.streaming.util.serialization.JSONKeyValueDeserializationSchema;

import java.util.Properties;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/9/14 14:00
 */
public class Flink03_Source_kafka {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(2);
    
        Properties props = new Properties();
        props.setProperty("bootstrap.servers", "hadoop162:9092,hadoop163:9092,hadoop164:9092");
        props.setProperty("group.id", "Flink03_Source_kafka");
        props.setProperty("auto.reset.offset", "latest");
    
        /*DataStreamSource<String> stream = env.addSource(
            new FlinkKafkaConsumer<String>(
                "s1", new SimpleStringSchema(), props
            )
        );*/
    
        DataStreamSource<ObjectNode> stream = env.addSource(
            new FlinkKafkaConsumer<>(
                "s1",
                new JSONKeyValueDeserializationSchema(true),
                props
            )
        );
    
        stream
            //.map(obj -> obj.get("value").get("name"))
            .print();
    
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
